package com.proyecto.track.bo;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;

@Entity
public class Project {

	private Long id;
	private String name;
	private Double hours;
	private ProjectStatus projectStatus;
	private UserPerson leader;
	private Period period;
	private List<ProjectUser> projectUsers = new ArrayList<ProjectUser>();
	private List<Task> tasks = new ArrayList<Task>();
	
	public Project(){
		
	}
	
	public Project(String name, Double hours){
		this.name = name;
		this.hours = hours;
	}

	@Id
	@GeneratedValue
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public Double getHours() {
		return hours;
	}
	public void setHours(Double hours) {
		this.hours = hours;
	}
	
	@OneToMany(mappedBy = "project", cascade={CascadeType.ALL})
	public List<ProjectUser> getProjectUsers() {
		return projectUsers;
	}
	public void setProjectUsers(List<ProjectUser> projectUsers) {
		this.projectUsers = projectUsers;
	}
	
	@OneToOne(cascade={CascadeType.ALL})
	public Period getPeriod() {
		return period;
	}

	public void setPeriod(Period period) {
		this.period = period;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	public UserPerson getLeader() {
		return leader;
	}

	public void setLeader(UserPerson leader) {
		this.leader = leader;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	public ProjectStatus getProjectStatus() {
		return projectStatus;
	}

	public void setProjectStatus(ProjectStatus projectStatus) {
		this.projectStatus = projectStatus;
	}

	@OneToMany(fetch = FetchType.LAZY)
	@JoinColumn(name="idProject")
	public List<Task> getTasks() {
		return tasks;
	}

	public void setTasks(List<Task> tasks) {
		this.tasks = tasks;
	}

	public void removeUser(ProjectUser projectUsersDisable) {
		projectUsersDisable.setActive(false);
	}

	public void addUsers(UserPerson userPerson) {
		if (userPerson.userPersonIsInProject(this.id)) {
			userPerson.getProjectUsersById(this.id).setActive(true);			
		} else {
			ProjectUser projectUser = new ProjectUser(true, this, userPerson);
			this.projectUsers.add(projectUser);
			userPerson.getProjectUsers().add(projectUser);
		} 
	}



	
}
